const BaseService = require('./BaseService.js');
const PageList = require('../model/PageList.js');

class RoomInfoServise extends BaseService {
  constructor() {
    super();
    this.currentTableName = this.tableMap.room_info;
  }
  /**
   * @description 分页查询
   * @param {{ room_name, pageIndex }} param 请求参数
   * @returns {Promise<PageList>} 返回分页查询以后的封装的PageList
   */
  async getRoomListByName({ room_name, pageIndex }) {
    let strSql = ` select * from ${this.currentTableName} where isDel = 0 `;
    let countSql = ` select count(*) 'total_count' from ${this.currentTableName} where isDel = 0 `;
    let strWhere = `  `;
    let ps = [];
    if (room_name) {
      strWhere += ` and room_name like ? `;
      ps.push(`%${room_name}%`);
    }
    strSql += ` ${strWhere} limit ${(pageIndex - 1) * this.pageSize}, ${this.pageSize} `;
    countSql += strWhere;
    let results = await this.executeSql(strSql + ';' + countSql, [...ps, ...ps]);
    let pageList = new PageList(pageIndex, results[1][0].total_count, this.pageSize, results[0]);
    return pageList;
  }

  exportExcel({ room_name }) {
    let strSql = ` select * from ${this.currentTableName} where isDel = 0 `;
    let strWhere = `  `;
    let ps = [];
    if (room_name) {
      strWhere += ` and room_name like ? `;
      ps.push(`%${room_name}%`);
    }
    strSql += strWhere;
    return this.executeSql(strSql, ps);
  }

  /**
   * @description 新增房间数据
   * @param {{ room_name, max_count, kt, network, washroom, room_size }} param 新增的删除
   * @returns {Promise<boolean>} true代表新增成功,false代表新增失败
   */
  async addRoomInfo({ room_name, max_count, kt, network, washroom, room_size }) {
    let strSql = ` INSERT INTO ${this.currentTableName} (room_name, max_count, kt, network, washroom, room_size) VALUES (?, ?, ?, ?, ?, ?); `;
    let results = await this.executeSql(strSql, [
      room_name,
      max_count,
      kt,
      network,
      washroom,
      room_size
    ]);
    return results.affectedRows > 0 ? true : false;
  }

  /**
   * @description 验证某一个房间编号是否存在
   * @param {string} room_name 要验证的房间编号
   * @returns {Promise<boolean>} true代表存在，false代表不存在
   */
  async existsRoomName(room_name) {
    let strSql = ` select * from ${this.currentTableName} where isDel = 0 and room_name = ? `;
    let results = await this.executeSql(strSql, [room_name]);
    return results.length > 0 ? true : false;
  }

  /**
   * @description 编辑房间数据
   * @param {{ id,room_name, max_count, kt, network, washroom, room_size }} param 新增的删除
   * @returns {Promise<boolean>} true代表编辑成功,false代表编辑失败
   */
  async updataRoomInfo({ id, room_name, max_count, kt, network, washroom, room_size }) {
    let strSql = ` UPDATE  ${this.currentTableName}  SET  room_name  = ?,  max_count  = ?,  kt  = ?,  network  = ?,  washroom  = ?,  room_size  = ? WHERE isDel = 0 and id = ?;
    `;
    let results = await this.executeSql(strSql, [
      room_name,
      max_count,
      kt,
      network,
      washroom,
      room_size,
      id
    ]);
    return results.affectedRows > 0 ? true : false;
  }
}

module.exports = RoomInfoServise;
